package top.suven.base.http.jetty.settings;


import top.suven.base.util.constants.GlobalConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration("jettyServer")
@ConfigurationProperties(prefix = GlobalConstants.JETTY_SERVER_SETTINGS)
public class JettyServerSettings {

    private String documentRoot = "/data/server/";
    private JettyLowResourceMonitorSettings lowResources = new JettyLowResourceMonitorSettings();
    private JettyThreadPoolSettings threadPool = new JettyThreadPoolSettings();

    public JettyLowResourceMonitorSettings getLowResources() {
        return lowResources;
    }
    public void setLowResources(JettyLowResourceMonitorSettings lowResources) {
        this.lowResources = lowResources;
    }
    public JettyThreadPoolSettings getThreadPool() {
        return threadPool;
    }
    public void setThreadPool(JettyThreadPoolSettings threadPool) {
        this.threadPool = threadPool;
    }

    public String getDocumentRoot() {
        return "";
//        return documentRoot;
    }

    public void setDocumentRoot(String documentRoot) {
        if(null != documentRoot && !"".equals(documentRoot))
        this.documentRoot = documentRoot;
    }

    @Override
    public String toString() {
        return "JettyServerSettings{" +
            " documentRoot=" + documentRoot +
            ", lowResources=" + lowResources.toString()+
            ", threadPool=" + threadPool.toString() +
            '}';
    }

    public static  class JettyLowResourceMonitorSettings {
        private int period = 1000;
        private int idleTimeout = 200;
        private boolean monitorThreads = true;
        private int maxConnections = 0;
        private int maxMemory;
        private int maxLowResourcesTime = 5000;

        public int getPeriod() {
            return period;
        }

        public void setPeriod(int period) {
            this.period = period;
        }

        public int getIdleTimeout() {
            return idleTimeout;
        }

        public void setIdleTimeout(int idleTimeout) {
            this.idleTimeout = idleTimeout;
        }

        public boolean isMonitorThreads() {
            return monitorThreads;
        }

        public void setMonitorThreads(boolean monitorThreads) {
            this.monitorThreads = monitorThreads;
        }

        public int getMaxConnections() {
            return maxConnections;
        }

        public void setMaxConnections(int maxConnections) {
            this.maxConnections = maxConnections;
        }

        public int getMaxMemory() {
            return maxMemory;
        }

        public void setMaxMemory(int maxMemory) {
            this.maxMemory = maxMemory;
        }

        public int getMaxLowResourcesTime() {
            return maxLowResourcesTime;
        }

        public void setMaxLowResourcesTime(int maxLowResourcesTime) {
            this.maxLowResourcesTime = maxLowResourcesTime;
        }

        @Override
        public String toString() {
            return "JettyLowResourceMonitorSettings{" +
                "period=" + period +
                ", idleTimeout=" + idleTimeout +
                ", monitorThreads=" + monitorThreads +
                ", maxConnections=" + maxConnections +
                ", maxMemory=" + maxMemory +
                ", maxLowResourcesTime=" + maxLowResourcesTime +
                '}';
        }
    }

    public static class JettyThreadPoolSettings {
        private int minThreads = 8;
        private int maxThreads = 200;
        private int idleTimeout = 30000;
        private boolean detailedDump;

        public int getMinThreads() {
            return minThreads;
        }

        public void setMinThreads(int minThreads) {
            this.minThreads = minThreads;
        }

        public int getMaxThreads() {
            return maxThreads;
        }

        public void setMaxThreads(int maxThreads) {
            this.maxThreads = maxThreads;
        }

        public int getIdleTimeout() {
            return idleTimeout;
        }

        public void setIdleTimeout(int idleTimeout) {
            this.idleTimeout = idleTimeout;
        }

        public boolean isDetailedDump() {
            return detailedDump;
        }

        public void setDetailedDump(boolean detailedDump) {
            this.detailedDump = detailedDump;
        }

        @Override
        public String toString() {
            return "JettyThreadPoolSettings{" +
                "minThreads=" + minThreads +
                ", maxThreads=" + maxThreads +
                ", idleTimeout=" + idleTimeout +
                ", detailedDump=" + detailedDump +
                '}';
        }
    }
}
